﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
namespace cczEdit.DramaDialog
{
    public partial class EditDramaSetEquipmentDialog : Form
    {
        PListDict curDict;
        string defaultEuip = "默认装备";
        string unEqip = "卸下装备";
        public EditDramaSetEquipmentDialog()
        {
            InitializeComponent();
            
            comboBoxWeapon.Items.Add(defaultEuip);
            comboBoxWeapon.Items.Add(unEqip);
            comboBoxArmor.Items.Add(defaultEuip);
            comboBoxArmor.Items.Add(unEqip);
            comboBoxAssist.Items.Add(defaultEuip);
            comboBoxAssist.Items.Add(unEqip);
            
            foreach (Hero hero in UCDrama.heros)
            {
                comboBoxHero.Items.Add(hero.name);
            }
            foreach (Equipment weapon in UCDrama.weapons)
            {
                comboBoxWeapon.Items.Add(weapon.name);
            }
            foreach (Equipment armor in UCDrama.armors)
            {
                comboBoxArmor.Items.Add(armor.name);

            }
            foreach (Equipment assist in UCDrama.assists)
            {
                comboBoxAssist.Items.Add(assist.name);

            }
        }
        public void Show(IWin32Window owner, PListDict dict)
        {
            curDict = dict;
            listBoxHeros.Items.Clear();
            PListDict dataDict = (PListDict)dict["DataDic"];
            foreach (KeyValuePair<string, IPListElement> kv in dataDict)
            {
                Hero hero = UCDrama.heros.Find((h) => { return h.num == System.Int16.Parse(kv.Key); });
                listBoxHeros.Items.Add(hero.name);

            }
            if(listBoxHeros.Items.Count>0)
                listBoxHeros.SelectedIndex = 0;
            ShowDialog(owner);
        }
        private void listBoxHeros_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxHeros.SelectedItem == null)
                return;
            Hero hero = UCDrama.heros.Find((h) => { return h.name == listBoxHeros.SelectedItem.ToString(); });
            comboBoxHero.SelectedItem = hero.name;
            PListDict dataDict = (PListDict)curDict["DataDic"];
            PListDict heroDict = (PListDict)dataDict[hero.num.ToString()];
            PListInteger weaponID = (PListInteger)heroDict["WeaponID"];
            PListInteger weaponLv = (PListInteger)heroDict["WeaponLv"];
            PListInteger armorID = (PListInteger)heroDict["ArmorID"];
            PListInteger armorLv = (PListInteger)heroDict["ArmorLv"];
            PListInteger assistID = (PListInteger)heroDict["AssistID"];
            numericUpDownLvWpn.Value = weaponLv.Value;
            numericUpDownLvAmr.Value = armorLv.Value;
            if (weaponID.Value == -1)
                comboBoxWeapon.SelectedItem = unEqip;
            else if (weaponID.Value == -2)
                comboBoxWeapon.SelectedItem = defaultEuip;
            else
            {
                Equipment weapon = UCDrama.weapons.Find((i) => { return i.num == weaponID.Value; });
                comboBoxWeapon.SelectedItem = weapon.name;
            }
            if (armorID.Value == -1)
                comboBoxArmor.SelectedItem = unEqip;
            else if (armorID.Value == -2)
                comboBoxArmor.SelectedItem = defaultEuip;
            else
            {
                Equipment armor = UCDrama.armors.Find((i) => { return i.num == armorID.Value; });
                comboBoxArmor.SelectedItem = armor.name;
            }
            if (assistID.Value == -1)
                comboBoxAssist.SelectedItem = unEqip;
            else if (assistID.Value == -2)
                comboBoxAssist.SelectedItem = defaultEuip;
            else
            {
                Equipment assist = UCDrama.assists.Find((i) => { return i.num == assistID.Value; });
                comboBoxAssist.SelectedItem = assist.name;
            }
            
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            saveCurrentHeroData();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            Hero originHero = UCDrama.heros.Find((h) => { return h.name == listBoxHeros.Text.ToString(); });
            PListDict dataDict = (PListDict)curDict["DataDic"];
            dataDict.Remove(originHero.num.ToString());
            listBoxHeros.SelectedItem = null;
            listBoxHeros.Items.Remove(originHero.name);

        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Hero originHero = UCDrama.heros.Find((h) => { return h.name == listBoxHeros.Text.ToString(); });
            PListDict dataDict = (PListDict)curDict["DataDic"];
            dataDict.Remove(originHero.num.ToString());
            listBoxHeros.SelectedItem = null;
            listBoxHeros.Items.Remove(originHero.name);

            saveCurrentHeroData();
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            if (listBoxHeros.SelectedItem == null)
            {

                Hide();
                return;
            }
            Hero originHero = UCDrama.heros.Find((h) => { return h.name == comboBoxHero.Text.ToString(); });
            PListDict dataDict = (PListDict)curDict["DataDic"];
            dataDict.Remove(originHero.num.ToString());
            listBoxHeros.SelectedItem = null;
            listBoxHeros.Items.Remove(originHero.name);
            saveCurrentHeroData();
            this.Hide();
        }
        private void saveCurrentHeroData()
        {
            try { 
                Hero hero = UCDrama.heros.Find((h) => { return h.name == comboBoxHero.Text.ToString(); });
                listBoxHeros.Items.Add(hero.name);
                PListDict dataDict = (PListDict)curDict["DataDic"];
                PListDict heroDict = new PListDict();
                dataDict.Add(hero.num.ToString(), heroDict);
                heroDict["WeaponLv"] = new PListInteger((long)numericUpDownLvWpn.Value);
                heroDict["ArmorLv"] = new PListInteger((long)numericUpDownLvAmr.Value);
                if (comboBoxWeapon.SelectedItem.ToString() == unEqip)
                    heroDict["WeaponID"] = new PListInteger(-1);
                else if (comboBoxWeapon.SelectedItem.ToString() == defaultEuip)
                    heroDict["WeaponID"] = new PListInteger(-2);
                else
                {
                    Equipment weapon = UCDrama.weapons.Find((i) => { return i.name == comboBoxWeapon.SelectedItem.ToString(); });
                    heroDict["WeaponID"] = new PListInteger(weapon.num);
                }
                if (comboBoxArmor.SelectedItem.ToString() == unEqip)
                    heroDict["ArmorID"] = new PListInteger(-1);
                else if (comboBoxArmor.SelectedItem.ToString() == defaultEuip)
                    heroDict["ArmorID"] = new PListInteger(-2);
                else
                {
                    Equipment armor = UCDrama.armors.Find((i) => { return i.name == comboBoxArmor.SelectedItem.ToString(); });
                    heroDict["ArmorID"] = new PListInteger(armor.num);
                }
                if (comboBoxAssist.SelectedItem.ToString() == unEqip)
                    heroDict["AssistID"] = new PListInteger(-1);
                else if (comboBoxAssist.SelectedItem.ToString() == defaultEuip)
                    heroDict["AssistID"] = new PListInteger(-2);
                else
                {
                    Equipment assist = UCDrama.assists.Find((i) => { return i.name == comboBoxAssist.SelectedItem.ToString(); });
                    heroDict["AssistID"] = new PListInteger(assist.num);
                }
             }
            catch
            {
                Hero hero = UCDrama.heros.Find((h) => { return h.name == comboBoxHero.SelectedItem.ToString(); });
                listBoxHeros.Items.Remove(hero.name);
                MessageBox.Show("Data Error");
            }
        }
    }
}
